package com.fz.service.merchant.impl;

import com.fz.common.ResponseEntity;
import com.fz.dto.merchant.merMenu.MerMenuAdd;
import com.fz.dto.merchant.merMenu.MerMenuQuery;
import com.fz.dto.merchant.merMenu.MerMenuUpd;
import com.fz.entity.merchant.MerMenu;
import com.fz.mapper.merchant.MerMenuMapper;
import com.fz.service.merchant.MerMenuService;
import com.fz.vo.merchant.merMenu.MerMenuTreeVo;
import com.fz.vo.merchant.merMenu.MerMenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("unchecked")
public class MerMenuServiceImpl implements MerMenuService {

    private static final Logger logger = LoggerFactory.getLogger(MerMenuServiceImpl.class);
    @Resource
    private MerMenuMapper merMenuMapper;


    /**
     * @方法描述: 目录查询
     * @参数:
     * @返回值:
     * @创建人: shy
     * @创建时间:: 2021/3/18
     */
    @Override
    public ResponseEntity<MerMenuTreeVo> findMerMenu(MerMenuQuery merMenuQuery) {
        List<MerMenuVo> menuVoList = merMenuMapper.findMerMenuVoList(merMenuQuery);
        MerMenuTreeVo menuTreeVo = buildTree(menuVoList);
        return ResponseEntity.buildSuccess(menuTreeVo);
    }

    /**
     * @方法描述: 增加菜单目录
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/19
     */
    @Override
    public ResponseEntity addMerMenu(MerMenuAdd merMenuAdd) {
        MerMenu menu = new MerMenu();
        menu.setCache(merMenuAdd.getCache());
        menu.setComponent(merMenuAdd.getComponent());
        menu.setComponentName(merMenuAdd.getComponentName());
        menu.setHidden(merMenuAdd.getHidden());
        menu.setIcon(merMenuAdd.getIcon());
        menu.setIFrame(merMenuAdd.getIFrame());
        menu.setName(merMenuAdd.getName());
        menu.setPath(merMenuAdd.getPath());
        menu.setPermission(merMenuAdd.getPermission());
        menu.setPid(merMenuAdd.getPid());
        menu.setSort(merMenuAdd.getSort());
        menu.setType(merMenuAdd.getType());
        merMenuMapper.insert(menu);
        return ResponseEntity.buildSuccess();
    }


    /**
     * @方法描述: 修改菜单选项
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/19
     */
    @Override
    public ResponseEntity updateMerMenu(MerMenuUpd merMenuUpd) {
        MerMenu menu = new MerMenu();
        menu.setId(merMenuUpd.getId());
        menu.setCache(merMenuUpd.getCache());
        menu.setComponent(merMenuUpd.getComponent());
        menu.setComponentName(merMenuUpd.getComponentName());
        menu.setHidden(merMenuUpd.getHidden());
        menu.setIcon(merMenuUpd.getIcon());
        menu.setIFrame(merMenuUpd.getIFrame());
        menu.setName(merMenuUpd.getName());
        menu.setPath(merMenuUpd.getPath());
        menu.setPermission(merMenuUpd.getPermission());
        menu.setPid(merMenuUpd.getPid());
        menu.setSort(merMenuUpd.getSort());
        menu.setType(merMenuUpd.getType());
        merMenuMapper.updateById(menu);
        return ResponseEntity.buildSuccess();
    }


    @Override
    public MerMenuTreeVo findMerMenuTree() {
        List<MerMenuVo> all = merMenuMapper.findMerMenuTree();
        // 过滤出目录及菜单，转换成树结构
        List<MerMenuVo> list = all.stream().map(menu -> {
            MerMenuVo dto = new MerMenuVo();
            dto.setPid(menu.getPid());
            dto.setId(menu.getId());
            dto.setName(menu.getName());
            return dto;
        }).collect(Collectors.toList());
        all.clear();
        MerMenuTreeVo menuTreeVo = buildTree(list);
        return menuTreeVo;
    }


    /**
     * @方法描述: 菜单目录条件查询，并将其构造为树状
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/18
     */
    public static MerMenuTreeVo buildTree(List<MerMenuVo> merMenuVos) {
        List<MerMenuVo> trees = new ArrayList<>();
        Set<String> ids = new HashSet<>();
        merMenuVos.forEach(merMenuVo->{
            if (merMenuVo.getPid() == 0) {
                trees.add(merMenuVo);
            }
            for (MerMenuVo its : merMenuVos) {
                if (its.getPid().equals(merMenuVo.getId())) {
                    if (merMenuVo.getChildren() == null) {
                        merMenuVo.setChildren(new ArrayList<>());
                    }
                    logger.debug("prevent repeat");
                    merMenuVo.getChildren().add(its);
                    ids.add(its.getId().toString());
                }
            }
        });
        MerMenuTreeVo menuTreeVo = new MerMenuTreeVo();
        menuTreeVo.setMenuTree(trees);
        if(merMenuVos != null && merMenuVos.size() > 0){
            menuTreeVo.setMenuSize(merMenuVos.size());
        }else{
            menuTreeVo.setMenuSize(0);
        }
        return menuTreeVo;
    }
}
